package MailGenerator;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class Mailer {
	public static int sendMail(final String recipients, final String subject, final String body) {
		System.out.println(recipients);
		String senderEmail = "ubcdating@gmail.com";
		String password = "ubcdatingpass";
	    final Session session = Session.getInstance(System.getProperties(), null);
	    final Message msg = new MimeMessage(session);
	    Transport transport = null;
	    try {
	    msg.setFrom(new InternetAddress(senderEmail));
	
	    final Address[] recipientAddresses = InternetAddress.parse(recipients);
	    msg.setRecipients(Message.RecipientType.TO, recipientAddresses);
	
	    msg.setSentDate(new Date());
	    msg.setSubject(subject);
	    msg.setText(body);
	
	    transport = session.getTransport("smtps");
	    transport.connect("smtp.gmail.com", 465, senderEmail, password);
	    transport.sendMessage(msg, recipientAddresses);
	    transport.close();
	    } catch (Exception e){
	    	System.out.println(e.getMessage());
	    	if (transport!=null)
				try {
					transport.close();
				} catch (MessagingException e1) {
					// TODO Auto-generated catch block
					System.out.println(e1.getMessage());
				}
		    return -1;
	    }
	    return 1;
	}
}
